package com.lagou.payrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitConfig
 *
 * @author zyt
 * @since 2020/11/14 22:29
 */
@Configuration
public class RabbitConfig {

    /**
     * 定义待支付订单队列
     * @return
     */
    @Bean
    public Queue queue() {

        Map<String, Object> props = new HashMap<>();
        // 设置队列中的消息 过期时间 10秒
        props.put("x-message-ttl", 10 * 1000);
        // 设置关联的死信交换器
        props.put("x-dead-letter-exchange","ex.pay.dlx");
        // 设置关联的死信交换器的routingKey
        props.put("x-dead-letter-routing-key","key.pay.dlx");
        return new Queue("q.pay", false, false, false, props);
    }

    /**
     * 定义待支付订单 交换器
     * @return
     */
    @Bean
    public Exchange exchange() {
        return new CustomExchange("ex.pay", ExchangeTypes.DIRECT,false,false,null);
    }

    /**
     * 定义 待支付订单 交换器和队列的绑定
     * @return
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(exchange()).with("key.pay").noargs();
    }

    /**
     * 定义 死信队列
     * @return
     */
    @Bean
    public Queue queueDlx() {
        return new Queue("q.pay.dlx", false, false, false);
    }

    /**
     * 定义死信交换器
     * @return
     */
    @Bean
    public Exchange exchangeDlx() {
        return new DirectExchange("ex.pay.dlx",false,false,null);
    }

    /**
     * 定义死信队列 死信交换器绑定
     * @return
     */
    @Bean
    public Binding bindingDlx() {
        return BindingBuilder.bind(queueDlx()).to(exchangeDlx()).with("key.pay.dlx").noargs();
    }

}
